//////////////////////////////////////////////////
//                DsHelper.cs                   //
//           Uses Ultraxion button.             //
//////////////////////////////////////////////////

using System;
using System.Drawing;
using System.Linq;
using Styx;
using Styx.Helpers;
using Styx.Logic.Combat;
using Styx.Plugins.PluginClass;
using Styx.WoWInternals;
using Styx.WoWInternals.WoWObjects;

namespace DsHelper
{
    public class DsHelper : HBPlugin
    {
        public override Version Version { get { return new Version(1, 2); } }

        public override string Name { get { return "Dragon Soul Helper"; } }
        public override string Author { get { return "fiftypence"; } }

        public override void Initialize()
        {
            Logging.Write(Color.Orange, "[DSHelper] DSHelper Loaded");
        }

        public override void Pulse()
        {
            DoUltraxionHourOfTwilight();
            DoUltraxionFadingLight();
            DoDeathwingShrapnel();
        }

        private void DoUltraxionHourOfTwilight()
        {
            var ultraxion = ObjectManager.GetObjectsOfType<WoWUnit>(true).FirstOrDefault(unit => unit.IsCasting && unit.CastingSpell.Name == "Hour of Twilight");

            if (ultraxion != null && ultraxion.CurrentCastTimeLeft.TotalSeconds <= 1)
            {
                Logging.Write(Color.Orange, "[DSHelper] Ultraxion Button Pressed for Hour of Twilight");
                DoPressButton();
            }
        }

        private void DoUltraxionFadingLight()
        {
            if (StyxWoW.Me.Auras.ContainsKey("Fading Light") &&
                StyxWoW.Me.Auras["Fading Light"].TimeLeft.TotalSeconds <= 1)
            {
                Logging.Write(Color.Orange, "[DSHelper] Ultraxion Button Pressed for Fading Light");
                DoPressButton();
            }
        }

        private void DoDeathwingShrapnel()
        {
            var shrapnel = ObjectManager.GetObjectsOfType<WoWUnit>(true).FirstOrDefault(unit => unit.IsCasting && unit.CurrentTarget != null && 
                                                                                                unit.CurrentTarget.Guid == StyxWoW.Me.Guid && 
                                                                                                unit.CastingSpell.Name == "Shrapnel");

            if (shrapnel != null && shrapnel.CurrentCastTimeLeft.TotalSeconds <= 1.5)
            {
                Logging.Write(Color.Orange, "[DSHelper] Deathwing Button Pressed for Shrapnel");
                DoPressButton();
            }
        }

        private void DoPressButton()
        {
            SpellManager.StopCasting();
            Lua.DoString("RunMacroText('/click ExtraActionButton1');");
        }
    }
}